﻿using System;
//using System.Collections.Generic;
//using System.Linq;
//using System.Text;
using System.Security.Cryptography;
using System.IO;
using AMS.DeDuplicator.CommonLayer;

namespace AMS.DeDuplicator.BusinessLayer
{
    public abstract class Hasher : IHasher
    {
        // does this need to be private?
        protected HashAlgorithm _hasher;

        public Hasher(HashAlgorithm hashAlgorithm)
        {
            InputValidator.ValidateNotNull(hashAlgorithm);

            _hasher = hashAlgorithm;
        }

        public virtual string GetHashValue(string filePath)
        {
            // validate input
            InputValidator.ValidateNotNull(filePath);
            InputValidator.ValidateFileExists(filePath);

            // set up filestream
            FileStream fileStream = new FileStream(filePath, FileMode.Open);
            //FileStream fileStream = new FileInfo(filePath).Open(FileMode.Open);
            fileStream.Position = 0;

            // get hash
            byte[] hash = _hasher.ComputeHash(fileStream);

            // close filestream
            fileStream.Close();

            // convert to Base64 string and return
            return Convert.ToBase64String(hash);
        }
    }
}